<?php

namespace addons\yunfood\model;

use addons\yunfood\library\Logic;
use addons\yunfood\library\ModelTrait;
use addons\yunfood\library\PayService;
use think\Model;

/**
 * 订单退款模型
 */
class Refundment extends Model
{
    use ModelTrait;

    // 表名,不含前缀
    protected $name = 'yunfood_refundment';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    protected $resultSetType = 'collection';

    // 来源类型订单
    const FROM_TYPE_ORDER = 'order_refund';

    // 渠道
    const CHANNEL_WECHAT_MP      = 'wechat-mp';
    const CHANNEL_WECHAT_MINIAPP = 'wechat-miniapp';
    const CHANNEL_ALIPAY_SCAN    = 'alipay-scan';

    public static function getChannelList()
    {
        return [
            self::CHANNEL_WECHAT_MP      => __('微信H5支付'),
            self::CHANNEL_WECHAT_MINIAPP => __('微信小程序支付'),
            self::CHANNEL_ALIPAY_SCAN    => __('支付宝扫码支付')
        ];
    }

    public function getChannelTextAttr($value, $data)
    {
        $optionList = self::getChannelList();

        return $optionList[$data['channel']] ?? $data['channel'];
    }

    // 状态
    const STATUS_WAIT    = 1;
    const STATUS_DONE    = 2;
    const STATUS_INVALID = 3;
    const STATUS_FAIL    = 4;

    public static function getStatusList()
    {
        return [
            self::STATUS_WAIT    => __('待退款'),
            self::STATUS_DONE    => __('已退款'),
            self::STATUS_INVALID => __('已失效'),
            self::STATUS_FAIL    => __('已失败')
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }

    public static function submitRefund($orderRefund, $paymentNo)
    {
        $refundmentData = self::getRefundmentData($orderRefund, $paymentNo);
        PayService::submitRefundment($refundmentData);
        return $refundmentData;
    }

    public static function getRefundmentData($orderRefund, $paymentNo)
    {
        $payment = Payment::get(['payment_no' => $paymentNo]);
        $refundment = null;
        if ($orderRefund->refundment_no) {
            if ($refundment = Refundment::get(['refundment_no' => $orderRefund->refundment_no])) {
                if ($refundment->created_at < time() - 300) {
                    $refundment = null;
                } else {
                    $refundment->status = Refundment::STATUS_INVALID;
                    $refundment->save();
                }
            }
        }
        if ($refundment === null) {
            $refundment = new Refundment();
            $refundment->refundment_no = Logic::generateBusinessNo('refundment_no');
            $refundment->from_type = Refundment::FROM_TYPE_ORDER;
            $refundment->from_no = $orderRefund->refund_no;
            $refundment->trade_no = Logic::generateBusinessNo('trade_no');
            $refundment->out_trade_no = $payment->trade_no;
            $refundment->payment_amount = $payment->goods_amount;
            $refundment->channel = $payment->channel;
        }
        $refundment->refund_amount = $orderRefund->refund_amount_rel;
        $refundment->refund_remark = $orderRefund->comment;
        $refundment->request_time = time();
        $refundment->status = Refundment::STATUS_WAIT;
        $refundment->save();

        return $refundment;
    }
}
